/**
 * author:Administrator
 * date:2016/3/19
 * desc: order create saga
 *
 * domain event:
 * 1.category created => invalid cache
 */

'use strict';

let Saga = require('../../lib/Saga.js');
let events = require('./domainEvent.js');
let category = require('../../infrastructure/query/mysql/category.js');
let Model = require('../../domain/model_cmd/category/Category.js');
let co = require('co');

class CategoryCreateSaga extends Saga {
  constructor(pojo) {
    super();
    // console.log(this.constructor.name + ' set up...');
    super.subscribe(events.ENUM_EVENT.CategoryCreated);
    this.pojo = pojo;
    this.start();
  }

  // 启动
  start() {
    let loc = Model.freshNew(this.pojo);
    let self = this;
    co(function*() {
      yield loc.store(self.sagaId);
    });
  }

  /** 接收到 orderPlaced 事件的处理 */
  * onCategoryCreated(event) {
    // console.log('handle event ', event.type);
    // 更新缓存
    category.invalidCache(event.data);
    this._end(event.data);
  }
}

module.exports = CategoryCreateSaga;